﻿using System;
using System.Collections.Generic;
using System.Text;
using llAma.Base;
using llAma.Backend.Interface;
using dAmn;
using llamaServer;
using llAma.Interface;

namespace socialdeveloperBackend
{
    public class backendPropertyManager : ChannelPropertyManagerBase
    {
        private IChannel mChannel;
        public backendPropertyManager(IChannel thisChannel)
        {
            mChannel = thisChannel;
            this.Namespace = mChannel.Namespace;
        }
        [PropertyHandler("privclasses", PropertyHandlerMask.SETTER)]
        [PropertyHandler("privclasses", PropertyHandlerMask.GETTER)]
        [PropertyHandler("members", PropertyHandlerMask.GETTER)]
        [PropertyHandler("members", PropertyHandlerMask.SETTER)]
        protected void handleGetPrivclasses(Packet p, object sender, PropertyHandlerMask mask)
        {
            if (mask == PropertyHandlerMask.SETTER)
            {
                SendErrorPacketToClient((IConnection)sender, p, "read-only property");
                return;
            }
            switch (p.args["p"].ToLower())
            {
                case "privclasses":
                    //SendPrivclassPacket((IConnection)sender);
                    break;
                case "members":
                    SendMembersPacket((IConnection)sender);
                    break;
            }
        }
        private Packet getMembersPacket()
        {
            string[] userVars = { "usericon", "symbol", "realname", "typename", "gpc" };
            Packet p = Packet.EmptyPacket;
            Packet Ret = Packet.EmptyPacket;
            foreach (IConnection k in ((IChannel)mChannel).Connections)
            {
                p = Packet.EmptyPacket;
                p.cmd = "member";
                p.param = k.Username;
                //This is going to be the PITA one.
                for (int x = 0; x < userVars.Length; x++)
                {
                    p.args[userVars[x]] = k.CurrentUser.getInfo(userVars[x]);
                }
                p.body = Ret.ToString();
                Ret = p;
            }
            Packet p2 = Packet.EmptyPacket;
            p2.cmd = "property";
            p2.param = this.Namespace;
            p2.args["p"] = "members";
            p2.body = Ret.ToString();
            return p2;
        }
        protected override void SendPropertiesToClient(IConnection conn)
        {
            string[] propsToSend = { "title", "topic", "motd", "botdirective" };
            for (int x = 0; x < propsToSend.Length; x++)
                ((IPropertyManager)this).HandlePacket((Packet)(string.Format("get {0}\np={1}\n\n", this.Namespace, propsToSend[x])), conn);
            SendMembersPacket(conn);
        }
        protected void handlJoinPacket(IConnection conn)
        {

        }
        protected override IChannel MyChannel
        {
            get { return mChannel; }
        }
        private void SendMembersPacket(IConnection conn)
        {
            conn.Send(getMembersPacket().ToString());
        }
    }
    public class PChatChannel : ChannelBase
    {
        private string mUser1;
        private string mUser2;
        public static PChatChannel create(string user1, string user2)
        {
            PChatChannel ret = new PChatChannel();
            //Leave title and topic alone.
            //Just setup the partial namespace.
            ret.mUser1 = user1;
            ret.mUser2 = user2;
            ret.Namespace = string.Join(":", new string[] { "pchat", user1, user2 });
            return ret;
        }
        public PChatChannel()
            : base()
        {
        }
        public static bool ShouldExpire(PChatChannel p)
        {
            if (p != null)
                return p.CanExpire;
            return true;
        }
        public bool CanExpire
        {
            get { return this.GetAllConnectionsPresent().Length == 0; }
        }
        protected override bool CanUserJoin(IConnection sender)
        {
            if (string.Compare(sender.Username, mUser1, true) == 0 ||
                string.Compare(sender.Username, mUser2, true) == 0)
                return true;
            return false;
        }

        //In memory property manager.

        backendPropertyManager mMyManager = null;

        protected override llAma.Interface.IPropertyManager PropertyManager
        {
            get
            {
                if (mMyManager == null)
                    mMyManager = new backendPropertyManager(this);
                return mMyManager;

            }
        }

    }
}
